/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufba.lasid.virt;

import br.ufba.lasid.v1.jitclouds.mda.datatypes.BasicConfiguration;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.IConfiguration;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.virtualization.IHypervisor;
import br.ufba.lasid.v1.jitclouds.mda.datatypes.virtualization.IVirtualMachine;
import br.ufba.lasid.v1.utils.Constants;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.libvirt.Domain;
import org.libvirt.DomainInfo;
import org.libvirt.LibvirtException;

/**
 *
 * @author aliriosa
 */
public class VirtualMachine implements IVirtualMachine {

    IConfiguration config = null;
    Domain domain;

    public VirtualMachine() {
        config = new BasicConfiguration();
    }

    public VirtualMachine(Domain domain) {
        this.domain = domain;
    }

    public VirtualMachine(String URI, IConfiguration config) {
        this.config = config;
    }

    public void setDomain(Domain domain) {
        this.domain = domain;
    }

    public Domain getDomain() {
        return domain;
    }

    public IConfiguration getConfiguration() {
        return this.config;
    }

    public void setConfiguration(IConfiguration config) {
        this.config = config;
    }

    public IHypervisor getHypervisor() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setHypervisor(IHypervisor hypervisor) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void execute(String command) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void shutdown() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void startup() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void reboot() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void pause() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setStatus(VMStatus status) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public VMStatus getStatus() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setID(String ID) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getID() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setDescription(String description) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String getDescription() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void destroy() {
        try {
            System.out.println("Destroying Virtual Machine " + domain.getName());
            domain.destroy();
            System.out.println("Virtual Machine destroyed");
        } catch (LibvirtException ex) {
            Logger.getLogger(VirtualMachine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getDomainInfo() {
        try {
            DomainInfo df = domain.getInfo();
            return "Domain name = " + domain.getName() + " | " + Constants.IdParameter + " = "
                    + domain.getID() + " | " + Constants.OSTypeParameter + " = " + domain.getOSType() + " | MaxMemory = " + domain.getMaxMemory() + " | "
                    + domain.getMaxVcpus() + " | " + Constants.UUIDParameter + " = " + domain.getUUIDString() + " | Active? = " + domain.isActive()
                    + " | Persistent? = " + domain.isPersistent() + " | CPUTime = " + df.cpuTime + " | MaxMem = " + df.maxMem + " | memory = " + df.memory + " | nrVirtCpu = " + df.nrVirtCpu + " | state = " + df.state;
        } catch (LibvirtException ex) {
            Logger.getLogger(VirtualMachine.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
